package com.benson.classloader;

import java.io.IOException;
import java.io.InputStream;

public class ClassLoaderTest {

	/**
	 * 
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		ClassLoader classLoader = new ClassLoader() {
			@Override
			public Class<?> loadClass(String name) throws ClassNotFoundException{
				try {
					String fileName = name.substring(name.lastIndexOf(".")+1) + ".class";
					InputStream is = getClass().getResourceAsStream(fileName);
					if(is == null){
						return super.loadClass(name);
					}
					byte[] b = new byte[is.available()];
					is.read(b);
					Class<?> cls = defineClass(name, b,0,b.length);
					is.close();
					return cls;
				} catch (IOException e) {
					throw new ClassNotFoundException(e.getMessage());
				}
			}
		};
		//Object obj1 = classLoader.loadClass("java.lang.Object").newInstance();
		java.lang.Object obj = classLoader.loadClass("com.benson.classloader.ClassLoaderTest").newInstance();
		
		System.out.println(obj.getClass());
		System.out.println(obj instanceof com.benson.classloader.ClassLoaderTest);
	}
}
